package com.friends.utils.excel.easyexcel2;

import com.alibaba.excel.write.metadata.holder.WriteSheetHolder;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.DataFormat;
import org.apache.poi.ss.usermodel.Workbook;

public class WriteStyleInstance {
    private static WriteStyleInstance instance;

    private Workbook workbook;
    private CellStyle style;
    private DataFormat dataFormat;

    private WriteStyleInstance(WriteSheetHolder writeSheetHolder) {
        if (workbook == null) {
            //设置单元格格式为文本
            Workbook workbook = writeSheetHolder.getSheet().getWorkbook();
            CellStyle cellStyle = workbook.createCellStyle();
            DataFormat dataFormat = workbook.createDataFormat();
            cellStyle.setDataFormat(dataFormat.getFormat("@"));
            style = cellStyle;
        }
    }

    public synchronized static WriteStyleInstance getInstance(WriteSheetHolder writeSheetHolder) {
        if (instance == null && writeSheetHolder != null) {
            instance = new WriteStyleInstance(writeSheetHolder);
        }

        return instance;
    }

    public Workbook getWorkbook() {
        return workbook;
    }

    public CellStyle getCellStyle() {
        return style;
    }

    public DataFormat getDataFormat() {
        return dataFormat;
    }
}
